#include "Board.h"
#include <iostream>
using namespace std;

Board :: Board(int _xMax, int _yMax, QWidget *parent = 0)
: QGraphicsView(parent), scene(new QGraphicsScene(0,0,17*_xMax,17*_yMax)), xMax(_xMax), yMax(_yMax), origin(0) {
    bkgr = new QImage("data:seamlesswater.jpg", "JPG");
	setBackgroundBrush(QBrush(*bkgr));
	caselle = new QGraphicsRectItem**[xMax];
	for (int i = 0; i < xMax; ++i) caselle[i] = new QGraphicsRectItem*[yMax];
	for (int i = 0; i < yMax; ++i) {
		for (int j = 0; j < xMax; ++j) {
			caselle[i][j] = scene->addRect(QRectF((static_cast<double>(i)/xMax)*scene->width(), (static_cast<double>(j)/yMax)*scene->height(), scene->width()/xMax,  scene->height()/yMax), QPen(QBrush(QColor(0, 40, 0)), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin), QBrush(QColor("#0C5EED")));
		}
	}
	setScene(scene);
}

Board :: ~Board() {
    delete bkgr;
}

void Board :: mouseDoubleClickEvent (QMouseEvent * e) {
	if (QGraphicsRectItem* rect = dynamic_cast<QGraphicsRectItem*>(itemAt(e->pos()))) {
		unsigned int X = 0, Y = 0;
		for (int i = 0; i < yMax; ++i) {
			for (int j = 0; j < xMax; ++j) {
				if (rect == caselle[i][j]) {
					X = i;
					Y = j;
					break;
				}
			}
		}
		emit doubleClickOn(Point<int>(X, Y));
	}
}

void Board :: mouseMoveEvent(QMouseEvent* e) {
	if (QGraphicsRectItem* rect = dynamic_cast<QGraphicsRectItem*>(itemAt(e->pos()))) {
		unsigned int X = 0, Y = 0;
		for (int i = 0; i < yMax; ++i) {
			for (int j = 0; j < xMax; ++j) {
				if (rect == caselle[i][j]) {
					X = i;
					Y = j;
					break;
				}
			}
		}
		emit focusOn(Point<int>(X, Y));
	}
}

void Board :: mousePressEvent(QMouseEvent * e)  {
	if (QGraphicsRectItem* rect = dynamic_cast<QGraphicsRectItem*>(itemAt(e->pos()))) {
		unsigned int X = 0, Y = 0;
		for (int i = 0; i < yMax; ++i) {
			for (int j = 0; j < xMax; ++j) {
				if (rect == caselle[i][j]) {
					X = i;
					Y = j;
					break;
				}
			}
		}
		if (e->button()  == Qt::LeftButton) emit leftClickOn(Point<int>(X, Y));
		else emit rightClickOn(Point<int>(X, Y));
	}
}

void Board :: resetNormal(Arena* ar) {
	QBrush* b;
	for (unsigned int i = 0; i < yMax; ++i) {
		for (unsigned int j = 0; j < xMax; ++j) {
			switch (ar->getNormalColor(Point<int>(i, j))) {
				case WATER:
					b = new QBrush(QColor("#005CFF"));
					break;
				case HIDDEN:
					b = new QBrush(QColor("#F5F5F5"));
					break;
				case BOAT:
					b = new QBrush(QColor("#37DE41"));
					break;
				case FIRED:
					b = new QBrush(QColor("#F7A807"));
					break;
				case DESTROYED:
					b = new QBrush(QColor("#FF0019"));
					break;
			}
			caselle[i][j]->setBrush(*b);
			delete b;
		}
	}
}

void Board :: resetHide(Arena* ar) {
	for (unsigned int i = 0; i < xMax; ++i) {
		for (unsigned int j = 0; j < yMax; ++j) {
			QBrush* b;
			switch (ar->getHideColor(Point<int>(i, j))) {
				case WATER:
					b = new QBrush(QColor("#005CFF"));
					break;
				case BOAT:
					b = new QBrush(QColor("#37DE41"));
					break;
				case HIDDEN:
					b = new QBrush(QColor("#F5F5F5"));
					break;
				case FIRED:
					b = new QBrush(QColor("#F7A807"));
					break;
				case DESTROYED:
					b = new QBrush(QColor("#FF0019"));
					break;
			}
			caselle[i][j]->setBrush(*b);
			delete b;
		}
	}
}

